package DBLayer;
import java.sql.*;
import java.util.ArrayList;
import ModelLayer.GunReplicas;

public class DBGunreplicas implements IFDBGunreplicas{
	private Connection con;
	public DBGunreplicas(){
		con = DBConnection.getInstance().getDBcon();
	}
	public ArrayList<GunReplicas> getAllGunReplicas() {
		return miscWhere("");
	}
	public GunReplicas findGunReplica(int gunReplicasID) {
		String wClause = "gunreplicasID = "+gunReplicasID;
		return singleWhere(wClause);
	}
	public void insertGunReplica(GunReplicas gunReplica) throws SQLException {
		String query="INSERT INTO GunReplicas(gunreplicasID, fabric, calibre)  VALUES("+
				gunReplica.getProductID()  + ",'"  +
				gunReplica.getfabric()  + "','"  +
				gunReplica.getCalibre() + "')";
		                      
		       System.out.println("insert : " + query);	
		       try{ 
		            Statement stmt = con.createStatement();
		 	 	  	stmt.setQueryTimeout(5);
		 	 	  	stmt.executeUpdate(query);
		 	 	  	stmt.close();
		 		}	
		       
		 	 	catch(SQLException ex){
		 	 	  	System.out.println("Insert exception in gun replicas db: "+ex);
		 	   }
	}
	public void updategunReplica(GunReplicas gunReplica) throws SQLException {
		String query="UPDATE GunReplicas SET " +
    			"fabric = '"+gunReplica.getfabric()+"'" +
    			"calibre = '"+gunReplica.getCalibre()+"'" +
    			"WHERE gunreplicasID = "+gunReplica.getProductID();
    	                      
    	       System.out.println("Update : " + query);	
    	       try{ 
    	            Statement stmt = con.createStatement();
    	 	 	  	stmt.setQueryTimeout(5);
    	 	 	  	stmt.executeUpdate(query);
    	 	 	  	stmt.close();
    	 		}	
    	       
    	 	 	catch(Exception ex){
    	 	 	  	System.out.println("Update exception in gun replicas db: "+ex);
    	 	   }
	}
	private GunReplicas singleWhere(String wClause)
  	{
  		ResultSet results;
  		GunReplicas gunreplicaObj = new GunReplicas();
  	    String query =  buildQuery(wClause);
          System.out.println(query);
          
  		try{ 
  	 		Statement stmt = con.createStatement();
  	 		stmt.setQueryTimeout(5);
  	 		results = stmt.executeQuery(query);
  	 		
  	 		if( results.next() ){
  	 			gunreplicaObj = buildGunreplica(results);
                              stmt.close();
  			}
  		
  		}//end try	
  	 	catch(Exception e){
  	 		System.out.println("Query exception: "+e);
  	 	}
  		return gunreplicaObj;
  	}
	private ArrayList<GunReplicas> miscWhere(String wClause){
		ResultSet results;
		ArrayList<GunReplicas> list = new ArrayList<GunReplicas>();
		
		String query = buildQuery(wClause);
		try{
			Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
			while(results.next()){
				GunReplicas replicaObj = new GunReplicas();
				replicaObj = buildGunreplica(results);
				list.add(replicaObj);
			}
			stmt.close();
		}catch(Exception ex){
			System.out.println("Query exception: "+ex);
		}
		return list;
	}
	private String buildQuery(String wClause){
		String query = "SELECT * from GunReplicas ";
		if(wClause.length()>0)
			query = query+"WHERE "+ wClause;
		return query;
	}
	private GunReplicas buildGunreplica(ResultSet results){
		GunReplicas replicaObj = new GunReplicas();
		try{
			replicaObj.setFabric(results.getString(1));
			replicaObj.setCalibre(results.getDouble(2));
		}catch(Exception ex){
			System.out.println("Error in building gun replica");
			ex.printStackTrace();
		}
		return replicaObj;
	}
}